<?php

class AuthentificationController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
    }

    public function loginAction() {
        $form = new Application_Form_login();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formdata = $this->getRequest()->getPost();
            if ($form->isValid($formdata)) {
                $userName = $form->getValue('userName');
                $userPwd = $form->getValue('password');
                $authAdapter = $this->getAuthAdapter();
                $authAdapter->setIdentity($userName)
                        ->setCredential($userPwd);
                //->setCredentialTreatment('MD5(?)');

                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);

                if ($result->isValid()) {
                    $identity = $authAdapter->getResultRowObject();
                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);
                    $this->_redirect(visiteur);
                } else {
                    $this->view->errorMessage = 'echec d\'authentification';
                }
            }
        }
    }

    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('Authentification/login');
    }

    private function getAuthAdapter() {
        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
        $authAdapter->setTableName('comptable')
                ->setIdentityColumn('login')
                ->setCredentialColumn('mdp');
        return $authAdapter;
    }

}

